package org.djw.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.djw.basic.util.AjaxResult;
import org.djw.basic.util.PageList;
import org.djw.system.domain.Detail;
import org.djw.system.query.DetailQuery;
import org.djw.system.query.TypeQuery;
import org.djw.system.service.IDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import java.util.List;

/**
 * 明细管理接口
 */
@RestController
@RequestMapping("/detail")
@Api(tags = "明细接口",description = "明细接口详细描述")
public class DetailController {
    @Autowired
    private IDetailService detailService;

    /*
    查询一条,全部
    分页加高级查询
    新增或者修改
    删除
     */

    /**
     * 查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Detail queryById(@PathVariable("id") Long id){
        return detailService.queryById(id);
    }

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping("/list")
    public List<Detail> queryAll(){
        return detailService.queryAll();
    }

    /**
     * 根据类型查询
     * @param query
     * @return
     */
    @PostMapping("/pageType")
    public PageList<Detail> queryPageType(@RequestBody DetailQuery query){
        return detailService.queryPageType(query);
    }

    /**
     * 分页高级查询
     * @param query
     * @return
     */
    @PostMapping("/page")
    public PageList<Detail> queryPage(@RequestBody DetailQuery query){
        return detailService.queryPage(query);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            detailService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后再试");
        }
    }

    /**
     * 新增或者修改
     * @param detail
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "明细添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Detail detail){
        try {
            if (detail.getId() == null){
                detailService.add(detail);
            } else {
                detailService.update(detail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后再试");
        }
    }
}
